Q1:

Implement a string class. The Sting class is a wrapper for a char*. Implement the copy constructor (1), destructor (2) and method length (3). Overload operator<< (4) and operator+ (5). For strings

      s1("hello"), s2(" world") s3 = s1 + s2;
   

s3 becomes "hello world", while s1 and s2 remain unchanged. operator<< prints the string to an output stream. The implementation of the default constructor, should help you understand how an instance of class String is represented. You may use:

    
    int strlen(char* s);             // returns the length of s
    void strcpy(char* s1, char* s2); // copies the characters from
                                     // s2 into s1
    
 
     class String {
        private:
           char* s;  // 0 byte terminated, dynamic array of characters
        public:
 
       String(char* =NULL);    // default and one argument constructor
 
            // IMPLEMENT THE METHODS BELOW:
            String(const String&);  // copy constructor
            // return a new string, which is obtained by
            // appending the argument string to a copy of s
            String operator+(const String&) const;
            ~String();              // destructor
            int length() const;     // length of string
 
        friend ostream& operator<<(ostream&,const String&);
     };
 
 
     String::String(char* str) {
        if (str == NULL) {
           s = str;
           return;
        }
        s = new char[strlen(str)+1];
        strcpy(s,str);
     }
 
Q2:

Implement

       bool find_digit(unsigned int n, unsigned int k);
   

which returns true if and only if digit k occurs in n.

(Hint: for integers a = 7 ,b = 3,c
c = a % b // modulus, c = 1
c = a / b // integer division, c = 2
)

 

Q3:

 

Implement a recursive function

   int sum(int b[], int k)

 

which calculates the sum of the elements stored in array b of length k

You may assume k >= 1

 

 

Q4

Give the output of the following program segment!

To receive partial marks, indicate where p1, p2, pp1 and pp2 point after each assignment, in case you get the output wrong!

 
       int a = 7, b = 3,
       *p1, *p2,
       **pp1, **pp2;
 
  p1 = &a;                 // p1->a
  p2 = &b;                 // p2->b
  pp1 = &p1;               // pp1->p1 
  pp2 = &p2;               // pp2->p2
 
  **pp1 = **pp2 + b;       // a = b + b = 6
 
  cout << "a = " << a << " b = " << b << endl;
 
  *pp2  = &a;              // p2->a
  **pp1 = *p2 - b;         // a = a - b = 3
 
  cout << "a = " << a << " b = " << b << endl;
 
  p2 = *pp1;               // p2 = p1 -> a
  b = *p1 - **pp2;         // b = a - a = 0
 
 
  cout << "a = " << a << " b = " << b << endl;
    

Q5

Define and implement a queue container class. A queue has these operations:

The queue is to be implemented with templates. You may use an array or a linked implementation. First define the queue class and then implement the methods, and also implement

If Q is of type queue<int> then

                     Q.enqueue(5);
              Q.enqueue(4);
 
      cout << Q.top() << endl; // prints 5
      Q.dequeue();
      cout << Q.top() << endl; // prints 4